use([
    "public/javascripts/service/basic_infoService.js",
    "public/javascripts/widget/slider/slider.js",
    "public/javascripts/widget/prompt/prompt.js",
    "public/javascripts/widget/upload/upload.js",
    ], function(user_securityService, SliderComponent, PromptComponent, Upload) {
    var PageComponent = React.createClass({
        ouploads: null,
        componentDidMount: function() {
            this.getBasicInfo()
            this.ouploads = Upload.init({
                'selector':'#m-upload-one',
                'maxSize': 4*1024*1024,
                'multiple':false,    //是否多组图上传
                'limit': ['jpg', 'jpeg', 'png']
            });
        },
        receiveMsg: function(data) {

        },
        getInitialState: function () {
            // return this.props ? this.props : {};
            return {
                fullNameTip: false,
                emailTip: false,
                emailTipTxt: '电子邮箱格式不正确',
                phoneTip: false,
                phoneTipTxt: '请输入数字、空格和符号()-+',
            }
        },
        getBasicInfo: function () {
            var _this = this
            basic_infoService.getBasicInfo('', function (res) {
                if (res.errorCode === 0) {
                    _this.setState({
                        userId: res.data.id,
                        userName: res.data.userName,
                        userAvatar: res.data.userAvatar,
                        userAvatarID: res.data.userAvatarID,
                        fullName: res.data.fullName,
                        email: res.data.email,
                        mobilePhone: res.data.mobilePhone,
                    })
                }
            })
        },
        checkUserEmail: function (value) {
            var param = {
                email:value,
                userId: this.state.userId,
            }
            var _this = this;
            basic_infoService.checkUserEmail (param, function (res) {
                if (res.errorCode !== 0) {
                    _this.setState({
                        emailTip: true,
                        emailTipTxt: '电子邮箱已存在',
                    })
                }
            })
        },
        checkUserPhone: function (value) {
            var param = {
                mobilePhone: value,
                userId: this.state.userId,
            }
            var _this = this;
            basic_infoService.checkUserPhone (param, function (res) {
                if (res.data) {
                    _this.setState({
                        phoneTip: true,
                        phoneTipTxt: '手机号码已存在',
                    })
                }
            })
        },
        validateFullName: function (e) {
            var value = e.target.value;
            this.setState({ fullName: value })
            if (value.trim() && /^([A-Za-z\s\u4E00-\u9FA5]+)+$/.test(value)) {
                this.setState({ fullNameTip: false })
            } else {
                this.setState({ fullNameTip: true })
            }
        },
        validateEmail: function (e) {
            var value = e.target.value;
            this.setState({ email: value })
            if (value.trim() && /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value)) {
                this.setState({ emailTip: false });
                this.checkUserEmail(value);
            } else {
                this.setState({
                    emailTip: true,
                    emailTipTxt: '电子邮箱格式不正确'
                });
            }
        },
        validatePhone: function (e) {
            var value = e.target.value;
            this.setState({ mobilePhone: value });
            if (value.trim() && /^([\d\s\-\+\(\)]+)+$/.test(value)) {
                this.setState({ phoneTip: false })
                this.checkUserPhone(value);
            } else {
                this.setState({
                    phoneTip: true,
                    phoneTipTxt: '请输入数字、空格和符号()-+'
                });
            }
        },
        changeBasicInfo: function () {
            if (!this.state.fullName.trim() || !this.state.email.trim() || !this.state.mobilePhone.trim()) {
                return;
            }
            if (!/^([A-Za-z\s\u4E00-\u9FA5]+)+$/.test(this.state.fullName)) {
                this.setState({ fullNameTip: true });
                return;
            }
            if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(this.state.email)) {
                this.setState({
                    emailTip: true,
                    emailTipTxt:'电子邮箱格式不正确'
                });
                return;
            }
            if (!/^([\d\s\-\+\(\)]+)+$/.test(this.state.mobilePhone)) {
                this.setState({
                    phoneTip: true,
                    phoneTipTxt:'请输入数字、空格和符号()-+'
                });
                return;
            }
            var userAvatar = this.ouploads.getImgInfo();
            var params = {
                userName: this.state.userName,
                userAvatarID: userAvatar.id,
                fullName: this.state.fullName,
                email: this.state.email,
                mobilePhone: this.state.mobilePhone,
            }
            var _this = this;
            basic_infoService.changeBasicInfo(params, function (res) {
                if (res.errorCode === 0) {
                    PromptComponent.init({ type: 'success', msg: '修改成功' }).show();
                    _this.updateAvatar(userAvatar.path);
                } else {
                    PromptComponent.init({ type: 'warning', msg: res.msg }).show();
                    localStorage.removeItem('avatar');
                }
            })
        },
        updateAvatar: function (path) {
          console.log(path)
            var _this = this
            localStorage.setItem('avatar', path);
            $('#login .avatar').attr('src', path);
        },
        render:function() {
            var data = this.state.data;
            return ~init;
        }
    });
    if(typeof ReactDOM != "undefined") {
        ReactDOM.render(<PageComponent data={storage}/>, document.getElementById('container'));
    }
    module.exports = PageComponent;
});
